package com.example.androidpattern.proxy;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.androidpattern.AppConstant;
import com.example.androidpattern.EMTagHandler;
import com.example.androidpattern.R;
import com.example.androidpattern.proxy.classes.DynamicProxy;
import com.example.androidpattern.proxy.classes.ProxyImage;
import com.example.androidpattern.proxy.classes.RealImage;
import com.example.androidpattern.proxy.interfaces.IImage;

import java.lang.reflect.Proxy;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ProxyActivity extends AppCompatActivity {
    private static final String TAG = "ProxyActivity";
    @BindView(R.id.tv_proxy_info)
    TextView tvProxyInfo;
    @BindView(R.id.btn_proxy_static)
    Button btnProxyStatic;
    @BindView(R.id.btn_proxy_dynamic)
    Button btnProxyDynamic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_proxy);
        ButterKnife.bind(this);

        setTitle("代理模式");
        tvProxyInfo.setText(EMTagHandler.fromHtml(AppConstant.PROXY_DEFINE));

    }

    @OnClick({R.id.btn_proxy_static, R.id.btn_proxy_dynamic})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_proxy_static:
                ProxyImage proxyImage = new ProxyImage();
                proxyImage.show();
                break;
            case R.id.btn_proxy_dynamic:
                RealImage realImage = new RealImage();
                DynamicProxy dynamicProxy = new DynamicProxy(realImage);
                IImage instance = (IImage) Proxy.newProxyInstance(realImage.getClass().getClassLoader(),
                        realImage.getClass().getInterfaces(), dynamicProxy);
                Log.d(TAG, "onViewClicked: " + "show before");
                instance.show();
                Log.d(TAG, "onViewClicked: " + "show after");
                break;
        }
    }
}